<?php

namespace app\common\model;

class Goods extends TimeModel
{

    protected $name = "goods";

    protected $deleteTime = "delete_time";
    protected $defaultSoftDelete = 0;

    protected $hidden = ['delete_time', 'update_time'];

    public function getSkuJsonAttr($value)
    {
        return json_decode($value, true);
    }

    public function banner()
    {
        return $this->hasMany(GoodsBanner::class, 'goods_id', 'id');
    }

    public function sku()
    {
        return $this->hasMany(GoodsSku::class, 'goods_id', 'id');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }

    public static function getShopGoodsIdList($shopId)
    {
        //商家所有商品id

        //搜索店铺信息,判断是否展示 共享和 平台产品
//        $shop = Shop::find($shopId);
        //获取自营产品id数组
        $self = Goods::where('shop_id', $shopId)->column('id');

        $additional = ShopGoods::where('shop_id', $shopId)->where('status',1)->select();
        foreach ($additional as $v) {
            $self[] = $v->goods_id;
        }

//        //搜索 平台 和共享 产品id
//        //如果开启了 共享和平台
//        if ($shop->platform_goods_switch == 1 && $shop->share_goods_switch == 1) {
//            $additional = ShopGoods::where('shop_id', $shopId)->select();
//            foreach ($additional as $v) {
//                $self[] = $v->goods_id;
//            }
//        }
//        if ($shop->platform_goods_switch == 1 && $shop->share_goods_switch == 0) {
//            $additional = ShopGoods::where('shop_id', $shopId)->where('goods_shop_id', 0)->select();
//            foreach ($additional as $v) {
//                $self[] = $v->goods_id;
//            }
//        }
//        if ($shop->platform_goods_switch == 0 && $shop->share_goods_switch == 1) {
//            $additional = ShopGoods::where('shop_id', $shopId)->where('goods_shop_id', '<>', 0)->select();
//            foreach ($additional as $v) {
//                $self[] = $v->goods_id;
//            }
//        }
//        //搜索 商家其他库里的商品
//        $goods_library_id = GoodsLibraryShopRelation::where('shop_id', $shopId)->where('status', 1)->column('goods_library_id');
//        if($goods_library_id){
//            $re = GoodsLibraryGoodsRelation::where('goods_library_id','in',$goods_library_id)->where('status',1)->column('goods_id');
//            foreach ($re as $v){
//                $self[] = $v;
//            }
//        }
        return $self;
    }

}